package com.atguigu.service.Impl;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.AdminRoleDao;
import com.atguigu.dao.RoleDao;
import com.atguigu.entity.AdminRole;
import com.atguigu.entity.Role;
import com.atguigu.service.Roleservice;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.dc.pr.PRError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements Roleservice {

    @Autowired(required = false)
    private RoleDao roleDao;
    @Autowired
    private AdminRoleDao adminRoleDao;


    @Override
    protected BaseDao<Role> getEntityDao() {
        return roleDao;
    }

    @Override
    public List<Role> finAll() {
        return roleDao.findAll();
    }

    /*
     * 查找is_deleted = 1的角色
     * */
    @Override
    public List<Role> finAllisdelete() {
        return roleDao.findAllisdelete();
    }

    //更新is——deleted = 1 的角色
    @Override
    public void updateisdelte(Long id) {
        roleDao.updateisdelete(id);
    }

    @Override
    public Map<String, Object> findRoleByAdminId(Long id) {
        //查询到全部的用户权限
        List<Role> roleList = roleDao.findAll();
        //查询到点击的的用户的权限
        List<Long> rloeIdList = adminRoleDao.findRoleIdByAdminId(id);
        //用户已经拥有的权限的集合  显示在右边
        List<Role> assginRoleList = new ArrayList<>();
        //用户没有拥有的权限的集合   显示在左边
        List<Role> noAssginRoleList = new ArrayList<>();
        //创建一个集合将assginRoleList和noAssginRoleList当键存入map中
        Map<String, Object> map = new HashMap<>();
        //遍历所有的权限
        for (Role role : roleList) {
            //判断点击遍历的权限是否在查询到当前用户的权限中
            if (rloeIdList.contains(role.getId())) {
                //如果在的话就保存到assginRoleList中
                assginRoleList.add(role);
            } else {
                noAssginRoleList.add(role);
            }
            //将它两分别保存到request作用域当中
            map.put("assginRoleList", assginRoleList);
            map.put("noAssginRoleList", noAssginRoleList);
        }
        return map;
    }

    @Override
    public void saveUserRoleRealtionShip(Long adminId, Long[] rolesIds) {
        //将已经存在的权限进行逻辑删除
        adminRoleDao.deleteByAdminId(adminId);
        //遍历选中需要添加的权限的id
        for (Long rolesId : rolesIds) {
            //判断这个权限的饿id是否为空
            if(StringUtils.isEmpty(rolesId)) continue;
            //创建用户角色对象用来保存遍历的权限对应的id和用户的id
            AdminRole adminRole = new AdminRole();
           adminRole.setAdminId(adminId);
           adminRole.setRoleId(rolesId);
           //将adminRole的权限数据对象保存到数据库中
           adminRoleDao.insert(adminRole);
        }




















    }


}
